import java.util.ArrayList;
import java.util.Observable;
import java.util.Scanner;


public class PokerTable extends Observable {
	
	private Deck cards;
	private PokerGame game;
	private Player player;
	private Player dealer;
	
	public void playGame(int numberOfPlayers) {
		//...........................................................................................................setup	
		
				// Initialize game
				game = new PokerGame();
				
				// Get a deck of cards
				cards = new Deck();
				
				// Shuffle the deck
				cards.shuffle();
				
				// Create player
				player = new Player(1);
				
				// Create AI dealer
				dealer = new Player(0);
				
				roundComplete = false;
				
				deal(cards);
	}
	
	private boolean roundComplete = false;
	int dealerCardsToSwap = 0;
	
	public void cardSwap(boolean[] discardPile) {
		
		int discardCount = 0;
		
		// discard unwanted cards
		for (int i = 4; i >= 0; i--) {
			if (discardPile[i]) {
				player.removeCard(i);
				discardCount++;
			}
		}
		
		// deal replacement cards
		for (int j = 0; j < discardCount; j++) {
			player.addCard(cards.remove(0));
		}
		
		// Create AI for the dealer
		PokerAI AI = new PokerAI();
		
		// AI swaps cards
		dealerCardsToSwap = AI.play(dealer);
		
		for (int swapCount = 0; swapCount < dealerCardsToSwap; swapCount++) {
			dealer.addCard(cards.remove(0));
		}
		
		// Mark the round complete
		roundComplete = true;
		
		setChanged();
		notifyObservers();
	}
	
	public boolean roundComplete() {
		
		return roundComplete;
	}
	
	private int playerWins = 0;
	private int dealerWins = 0;
	
	public String getWinner() {
		
		ArrayList<Player> players = new ArrayList<Player>();
		players.add(player);
		players.add(dealer);
		
		Player winner = game.getWinner(players);
		
		if (winner.getSeat() == 0) {
			
			dealerWins++;
			return "Dealer Wins!";
		}
		
		else {
			
			playerWins++;
			return "You Win!";		
		}
	}
	
	public int getPlayerWins() {
		return playerWins;
	}
	
	public int getDealerWins() {
		return dealerWins;
	}
	
	public int getSwapResults() {
		
		return dealerCardsToSwap;
	}
	
	public Player getPlayer() {
		return player;
	}
	
	public Player getDealer() {
		return dealer;
	}
	
	private void deal(Deck deck) {
		
		// Deal player a hand of cards from the deck
			for (int iCardCount = 0; iCardCount < 5; iCardCount++) {
				player.addCard(cards.remove(0));
		}

		// Deal for the AI dealer
		for (int iCardCount = 0; iCardCount < 5; iCardCount++) {
			dealer.addCard(cards.remove(0));
		}
		
		setChanged();
		notifyObservers();
	}
	
	public String getDealerHand() {
		
		return game.evaluateHand(dealer).toString();
	}
	
	public String getDealerHighCard() {
		
		return game.getHighCard(dealer.getHand()).toString();
	}
	
	public String getPlayerHand() {
		
		return game.evaluateHand(player).toString();
	}
	
	public String getPlayerHighCard() {
		
		return game.getHighCard(player.getHand()).toString();
	}
				
		//..................................................................................................................based on player's choice, will need to redisplay (and notify?)
				// Display each player's hand and evaluation, let them discard cards, and display their
				// final hand and score
/*				for (Player player : players) {
					
					System.out.println("Player " + player.getSeat() + ":");
					int k = 1;
					for (Card card: player.getHand()) {
						System.out.println(k + ") " + card);
						k++;
					}
					System.out.println();
					System.out.println(game.evaluateHand(player));
					System.out.println("High card is " + game.getHighCard(player.getHand()));
					System.out.println();
					
					// Allow player to replace cards in hand with cards from deck
					System.out.println("Which cards do you want to get rid of?");
					Scanner keyboard = new Scanner(System.in);
					int cardChoice = keyboard.nextInt();
					
					int cardsSwapped = 0;
					int remainingCards = 5;
					while (cardChoice != 0 && remainingCards > 0) {
						if ((cardChoice > 0) && (cardChoice <= remainingCards)) {
							
							player.removeCard(cardChoice-1);
							cardsSwapped++;
							remainingCards--;
							
							// Display hand again
							int i = 1;
							for (Card card: player.getHand()) {
								System.out.println(i + ") " + card);
								i++;
							}
							
							System.out.println("Which cards do you want to get rid of?");
							cardChoice = keyboard.nextInt();
						}
						
						else {
							System.out.println("Invalid choice. Enter card number or 0 to stop");
							cardChoice = keyboard.nextInt();
						}
					}
					
					for (int replace = 0; replace < cardsSwapped; replace++) {
						player.addCard(cards.remove(0));
					}
					System.out.printf("Player %d discarded %d.\n\n", player.getSeat(), cardsSwapped);
					
					System.out.println("Player " + player.getSeat() + "'s final hand:");
					int i = 1;
					for (Card card: player.getHand()) {
						System.out.println(i + ") " + card);
						i++;
					}
					System.out.println();
					System.out.println(game.evaluateHand(player));
					System.out.println("High card is " + game.getHighCard(player.getHand()));
					System.out.println();
				}*/
}
